import sys
sys.path.append('../gen-py')

from com.demo.thrift import HelloWorldService
from com.demo.thrift.ttypes import *
from com.demo.thrift.constants import *

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer


class HelloWorldServiceHandler():
    def sayHello(self, username):
        resp = "Hi, " + username + " Welcome";
        print(resp)
        return resp


if __name__ == "__main__":
    handler = HelloWorldServiceHandler()
    processor = HelloWorldService.Processor(handler)
    transport = TSocket.TServerSocket(host='127.0.0.1', port=7099)
    tfactory = TTransport.TBufferedTransportFactory()
    #tfactory = TTransport.TFramedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    # You could do one of these for a multithreaded server
    # server = TServer.TThreadedServer(
    #     processor, transport, tfactory, pfactory)
    # server = TServer.TThreadPoolServer(
    #     processor, transport, tfactory, pfactory)

    print('Starting the server...')
    server.serve()
    print('done.')
